////////////////////////////////////////////////////////////////////////////////
// Copyright 2006 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   MyModule.java
// AUTHOR           :   Ric Yik
// DATE CREATE      :   2006/4/6
// DESCRIPTION      :   
//                  :   
//                  :   
//                  :   
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2006/05/26   Cyris Liang     1.0                 BugZilla #5868
// 2006/05/26   Ric Yik         1.0                 BugZilla #5868
//
//
////////////////////////////////////////////////////////////////////////////////

package com.erp.framework.model.ejb;

import java.sql.Connection;
import java.util.Collection;
import java.util.Iterator;

import javax.transaction.UserTransaction;

import com.erp.framework.exceptions.ModifyRecordException;
import com.erp.framework.model.dao.ServiceLocator;
import com.erp.framework.model.dao.SimplePageEditStyleDao;
import com.erp.framework.model.vo.SimpleBaseVo;
import com.erp.framework.util.logger.Logger;

public class SimplePageEditStyleEjb extends SimpleBaseSessionEjb {
    public void updateBatch(Collection masterCollection) throws ModifyRecordException {
        int i = 1;
        if (masterCollection == null || masterCollection.size() == 0)
            return;

        Connection conn = null;

        try {
            ServiceLocator srvLocator = new ServiceLocator();
            UserTransaction userTrans = srvLocator.getUserTransaction();

            SimplePageEditStyleDao dao = null;
            Iterator iterator = masterCollection.iterator();

            userTrans.begin();
            try {
                conn = srvLocator.getDSDBConnection();

                while (iterator.hasNext()) {
                    SimpleBaseVo masterModel = (SimpleBaseVo) iterator.next();
                    
                    /* Added by Alex 2006-02-21 for support of bizLogic in simplePageEditStyle.
                     *  ONLY bizLogicsBeforeCreateModel(), bizLogicsAfterCreateModel(), 
                     *   bizLogicsBeforeUpdateModel(), bizLogicsAfterUpdateModel(),
                     *   bizLogicsBeforeRemoveModel(), bizLogicsAfterRemoveModel()
                     *   are supported
                     */
                    getBizLogicTypeFromVo(masterModel);
                    // end Alex

                    if (dao == null) {
                        dao = (SimplePageEditStyleDao) masterModel.getDaoType().newInstance();
                    }

                    Logger.debug(this, "updateBatch(), master " + masterModel.getPrimaryKey() + ", exist = "
                            + masterModel.getIsExisting() + ", delete = " + masterModel.getIsDelete());
                    if (masterModel.getIsExisting()) {

                        if (!masterModel.getIsDelete()) {
                            // Case 1. updates an existing record
                        	if(bizLogic != null){
                                bizLogic.bizLogicsBeforeUpdateModel(masterModel, conn);
                            }
                            dao.updateModel(masterModel, conn);
                            if(bizLogic != null){
                                bizLogic.bizLogicsAfterUpdateModel(masterModel, conn);
                            }
                        } else {
                            // Case 2. deletes the whole branch
                        	if(bizLogic != null){
                                bizLogic.bizLogicsBeforeRemoveModel(masterModel, conn);
                            }
                            dao.removeModel(masterModel, conn);
                            if(bizLogic != null){
                                bizLogic.bizLogicsAfterRemoveModel(masterModel, conn);
                            }
                            iterator.remove();
                        }
                    } else {
                        if (!masterModel.getIsDelete()) {
                            // Case 3. inserts a record                        	
                            if(bizLogic != null){
                                bizLogic.genDocNo(masterModel, conn);
                            }
                            masterModel.assignKey(masterModel.getPrimaryKey());
                        	if(bizLogic != null){
                                bizLogic.bizLogicsBeforeCreateModel(masterModel, conn);
                            }
                            dao.insertModel(masterModel, conn);
                            if(bizLogic != null){
                                bizLogic.bizLogicsAfterCreateModel(masterModel, conn);
                            }
                        } else {
                            iterator.remove();
                        }
                    }
                }

                userTrans.commit();
            } catch (Exception e) {
                Logger.logExceptions(this, e);
                srvLocator.userTransactionRollBack(userTrans);
                throw new ModifyRecordException(e.getMessage());
            }
        } catch (java.lang.reflect.UndeclaredThrowableException ue) {
            Logger.logExceptions(this, ue);
            throw new ModifyRecordException(ue.getMessage());
        } catch (javax.transaction.RollbackException traxe) {
            Logger.logExceptions(this, traxe);
            throw new ModifyRecordException(traxe.getMessage());
        } catch (Exception e) {
            Logger.logExceptions(this, e);
            throw new ModifyRecordException(e.getMessage());
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
                Logger.logExceptions(this, e);
            }
        }
    }
}
